﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ECare.BUS.Workers;
using ECare.Client.templates;

using ECare.BUS.Data;
using ECare.BUS;

namespace ECare.Client.frms
{
    public partial class frmResourcesInfo : frmInfo
    {
        public frmResourcesInfo()
        {
            InitializeComponent();
            this.Load += frmResourcesInfor_Load;
        }

        void frmResourcesInfor_Load(object sender, EventArgs e)
        {
            //throw new NotImplementedException();

            this.LoadInformation();
        }
        private void LoadInformation()
        {
            if (this.entity == null)
                return;
            var resource = this.entity as ResoucesInfo;

            this.txtRcode.Text = resource.ResourceCode;
            this.txtRname.Text = resource.ResourceName;
            this.txtRunit.Text = resource.ResourceUnit;
            this.txtRts.Text = resource.ResourceTs;
            this.txtRregion.Text = resource.ResourceRegion;
            this.txtRvalidate.Value = resource.ResouceValidate;
            if (resource.ResouceImage != null)
            {
                this.pic.BackgroundImage = ImageHelper.GetImageFromBase64(resource.ResouceImage);
            }


        }

        private void eButton1_Click(object sender, EventArgs e)
        {
            var openimagedialog = new OpenFileDialog();
            openimagedialog.Filter = "All Image Files|*.bmp;*.ico;*.gif;*.jpeg;*.jpg;*.png;*.tif;*.tiff|"
+ "Windows Bitmap(*.bmp)|*.bmp|"
+ "JPEG File Interchange Format (*.jpg)|*.jpg;*.jpeg|"
+ "Portable Network Graphics (*.png)|*.png|"
+ "Tag Image File Format (*.tif)|*.tif;*.tiff";

            if (openimagedialog.ShowDialog() == DialogResult.OK)
            {
                var image = openimagedialog.FileName;
                pic.BackgroundImage = Bitmap.FromFile(image);
            }
        }
        public event EntitySavedHandler Saved;
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateTextControl(this.txtRcode) == false)
                return;
            if (ValidateTextControl(this.txtRname) == false)
                return;
            if (ValidateTextControl(this.txtRunit) == false)
                return;

            if (this.txtRvalidate.Value == 0)
            {
                this.errorProvider1.SetError(this.txtRvalidate, "?");
                return;
            }
            else {
                this.errorProvider1.SetError(this.txtRvalidate, "");
            }

            if (this.entity == null)
            {
                var newresource = new ResoucesInfo();
                newresource.ResourceCode = this.txtRcode.Text;
                newresource.ResourceName = this.txtRname.Text;
                newresource.ResourceUnit = this.txtRunit.Text;
                newresource.ResourceTs = this.txtRts.Text;
                newresource.ResourceRegion = this.txtRregion.Text;
                newresource.ResouceValidate =(int) this.txtRvalidate.Value;
                if (pic.BackgroundImage != null)
                {
                    newresource.ResouceImage = ImageHelper.ConverToBase64((Bitmap)pic.BackgroundImage);
                }
                var resourceId=ResourcesHelper.CreateResource(newresource);
                if (resourceId > 0)
                    newresource.ResourceId = resourceId;

                this.entity = newresource;
            }
            else
            {
                var newresource = this.entity as ResoucesInfo;
                newresource.ResourceCode = this.txtRcode.Text;
                newresource.ResourceName = this.txtRname.Text;
                newresource.ResourceUnit = this.txtRunit.Text;
                newresource.ResourceTs = this.txtRts.Text;
                newresource.ResourceRegion = this.txtRregion.Text;
                newresource.ResouceValidate = (int)this.txtRvalidate.Value;
                if (pic.BackgroundImage != null)
                {
                    newresource.ResouceImage = ImageHelper.ConverToBase64((Bitmap)pic.BackgroundImage);
                }
                ResourcesHelper.UpdateResource(newresource); 
            }
            if (this.Saved != null)
            {
                this.Saved(this.entity);
                this.Close();
            }


        }

        private void eButton1_Click_1(object sender, EventArgs e)
        {
            var worker = new ResourcesInfoReportBuilder { CurrentResourceInfo = this.entity as ResoucesInfo };
            worker.RunWorkerCompleted += worker_RunWorkerCompleted;
            worker.RunWorkerAsync();
            frmloading = new frmLoading();
            frmloading.ShowDialog();
        }
        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            frmloading.Close();

            var frmprint = new frmPrint(); frmprint.file = e.Result as string;
            frmprint.ShowDialog();
        }

        private void eButton2_Click(object sender, EventArgs e)
        {
            var worker = new ResourcesInfoReportBuilder { CurrentResourceInfo = this.entity as ResoucesInfo };
            worker.RunWorkerCompleted += worker_RunWorkerCompleted;
            worker.RunWorkerAsync();
            frmloading = new frmLoading();
            frmloading.ShowDialog();
        }

    }
}
